home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / MK_SAFE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  5.0 KB  |  172 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/krb/RCS/mk_safe.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * This routine constructs a Kerberos 'safe msg', i.e. authenticated
  12.  * using a private session key to seed a checksum. Msg is NOT
  13.  * encrypted.
  14.  *
  15.  *      Note-- bcopy is used to avoid alignment problems on IBM RT
  16.  *
  17.  *      Returns either <0 ===> error, or resulting size of message
  18.  *
  19.  * Steve Miller    Project Athena  MIT/DEC
  20.  */
  21.  
  22. #ifndef lint
  23. static char *rcsid_mk_safe_c=
  24. "$Header: mk_safe.c,v 4.12 89/03/22 14:50:49 jtkohl Exp $";
  25. #endif /* lint */
  26.  
  27. #include <mit_copy.h>
  28.  
  29. /* system include files */
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <sys\types.h>
  33. #include <netinet\in.h>
  34. #include <sys/socket.h>
  35.  
  36. /* application include files */
  37. #include <des.h>
  38. #include <krb.h>
  39. #include <prot.h>
  40. #include "lsbaddcm.h"
  41.  
  42. extern char *errmsg();
  43. extern int krb_debug;
  44.  
  45. /* static storage */
  46.  
  47. static u_long cksum;
  48. static C_Block big_cksum[2];
  49. static struct timeval msg_time;
  50. static u_char msg_time_5ms;
  51. static long msg_time_sec;
  52.  
  53. /*
  54.  * krb_mk_safe() constructs an AUTH_MSG_SAFE message.  It takes some
  55.  * user data "in" of "length" bytes and creates a packet in "out"
  56.  * consisting of the user data, a timestamp, and the sender's network
  57.  * address, followed by a checksum computed on the above, using the
  58.  * given "key".  The length of the resulting packet is returned.
  59.  *
  60.  * The "out" packet consists of:
  61.  *
  62.  * Size            Variable        Field
  63.  * ----            --------        -----
  64.  *
  65.  * 1 byte        KRB_PROT_VERSION    protocol version number
  66.  * 1 byte        AUTH_MSG_SAFE |        message type plus local
  67.  *            HOST_BYTE_ORDER        byte order in low bit
  68.  *
  69.  * ===================== begin checksum ================================
  70.  * 
  71.  * 4 bytes        length            length of user data
  72.  * length        in            user data
  73.  * 1 byte        msg_time_5ms        timestamp milliseconds
  74.  * 4 bytes        sender->sin.addr.s_addr    sender's IP address
  75.  *
  76.  * 4 bytes        msg_time_sec or        timestamp seconds with
  77.  *            -msg_time_sec        direction in sign bit
  78.  *
  79.  * ======================= end checksum ================================
  80.  *
  81.  * 16 bytes        big_cksum        quadratic checksum of
  82.  *                        above using "key"
  83.  */
  84.  
  85. long krb_mk_safe(in,out,length,key,sender,receiver)
  86.     u_char *in;            /* application data */
  87.     u_char *out;        /*
  88.                  * put msg here, leave room for header!
  89.                  * breaks if in and out (header stuff)
  90.                  * overlap
  91.                  */
  92.     u_long length;        /* of in data */
  93.     C_Block *key;        /* encryption key for seed and ivec */
  94.     struct sockaddr_in *sender;    /* sender address */
  95.     struct sockaddr_in *receiver; /* receiver address */
  96. {
  97.     register u_char     *p,*q;
  98.  
  99.     /*
  100.      * get the current time to use instead of a sequence #, since
  101.      * process lifetime may be shorter than the lifetime of a session
  102.      * key.
  103.      */
  104.     if (gettimeofday(&msg_time,(struct timezone *)0)) {
  105.         return  -1;
  106.     }
  107.     msg_time_sec = (long) msg_time.tv_sec;
  108.     msg_time_5ms = msg_time.tv_usec/5000; /* 5ms quanta */
  109.  
  110.     p = out;
  111.  
  112.     *p++ = KRB_PROT_VERSION;
  113.     *p++ = AUTH_MSG_SAFE | HOST_BYTE_ORDER;
  114.  
  115.     q = p;            /* start for checksum stuff */
  116.     /* stuff input length */
  117.     bcopy((char *)&length,(char *)p,sizeof(length));
  118.     p += sizeof(length);
  119.  
  120.     /* make all the stuff contiguous for checksum */
  121.     bcopy((char *)in,(char *)p,(int) length);
  122.     p += length;
  123.  
  124.     /* stuff time 5ms */
  125.     bcopy((char *)&msg_time_5ms,(char *)p,sizeof(msg_time_5ms));
  126.     p += sizeof(msg_time_5ms);
  127.  
  128.     /* stuff source address */
  129.     bcopy((char *) &sender->sin_addr.s_addr,(char *)p,
  130.           sizeof(sender->sin_addr.s_addr));
  131.     p += sizeof(sender->sin_addr.s_addr);
  132.  
  133.     /*
  134.      * direction bit is the sign bit of the timestamp.  Ok until
  135.      * 2038??
  136.      */
  137.     /* For compatibility with broken old code, compares are done in VAX 
  138.        byte order (LSBFIRST) */ 
  139.     if (lsb_net_ulong_less(sender->sin_addr.s_addr, /* src < recv */ 
  140.               receiver->sin_addr.s_addr)==-1) 
  141.         msg_time_sec =  -msg_time_sec; 
  142.     else if (lsb_net_ulong_less(sender->sin_addr.s_addr, 
  143.                 receiver->sin_addr.s_addr)==0) 
  144.         if (lsb_net_ushort_less(sender->sin_port,receiver->sin_port) == -1) 
  145.             msg_time_sec = -msg_time_sec; 
  146.     /*
  147.      * all that for one tiny bit!  Heaven help those that talk to
  148.      * themselves.
  149.      */
  150.  
  151.     /* stuff time sec */
  152.     bcopy((char *)&msg_time_sec,(char *)p,sizeof(msg_time_sec));
  153.     p += sizeof(msg_time_sec);
  154.  
  155. #ifdef NOENCRYPTION
  156.     cksum = 0;
  157.     bzero(big_cksum, sizeof(big_cksum));
  158. #else /* Do encryption */
  159.     /* calculate the checksum of length, timestamps, and input data */
  160.     cksum = quad_cksum(q,big_cksum,p-q,2,key);
  161. #endif /* NOENCRYPTION */
  162.     if (krb_debug)
  163.         printf("\ncksum = %u",cksum);
  164.  
  165.     /* stuff checksum */
  166.     bcopy((char *)big_cksum,(char *)p,sizeof(big_cksum));
  167.     p += sizeof(big_cksum);
  168.  
  169.     return ((long)(p - out));    /* resulting size */
  170.  
  171. }
  172.